﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using System.Web.Services;
using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class SysManage_User_Edit : System.Web.UI.Page
{
    BaimasoftFile MyFile = new BaimasoftFile();
    RoleManage MyRM = new RoleManage();
    DbRoleManage MyDRM = new DbRoleManage();
    UserManage MyUM = new UserManage();
    BenchmarkBLL bll = new BenchmarkBLL();

    public string UserRole = "", sDbRoleCode = "", sThisCode = "";


    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(SysManage_User_Edit));
        this.RoleName.Attributes.Add("onchange", "matchStationId(this.value)");

        UserRole = (string)Session["Baimasoft_Role_Code"];
        if (!Page.IsPostBack)
        {
            if (ViewState["VDbRoleCode"] == null || ViewState["VDbRoleCode"].ToString().CompareTo("") == 0)
            {
                ViewState["VDbRoleCode"] = "";  //数据角色
            }

            try
            {
                sDbRoleCode = Request.QueryString["DbRoleCode"].ToString();
            }
            catch
            {
                sDbRoleCode = "";
            }

            try
            {
                sThisCode = Request.QueryString["ThisCode"].ToString();   //用户组ID
                HThisCode.Value = sThisCode.Trim();
            }
            catch
            {
                sThisCode = "";
            }

            //if (aThisCode.Length < 2)
            //{
            //    return;
            //}

            RoleBind(UserRole);
            DbRoleBind(sDbRoleCode);
            AreaBind();

            string[] aThisCode = sThisCode.Split('^');

            if (aThisCode.Length >= 2)
            {
                ChoiceUserGroupName_1.Text = aThisCode[1].ToString().Trim();         //用户组名称


                HChoiceUserGroupID_1.Value = "1^" + aThisCode[0].ToString().Trim();  //用户组ID
            }

            HOrgIDCol.Value = AspNetHelper .GetRightOrgIds();   //初始化树的数据权限

           /* if ((string)Session["Baimasoft_User_LogonID"] != null)
            {
                HResourceOrgID.Value = Session["TD_ResourceOrgID"].ToString().Trim();
            }
            else
            {
                HResourceOrgID.Value = "";
            }*/
            //UserGroupBind();
            //FirstpageBind(UserRole);            
        }
    }


    [WebMethod]
    public static string[] getMatchStation(string sStation)
    {
        DataTable MyTb = (new DbRoleManage ()).GetMyDbRoleInfo (sStation);
        if (MyTb.Rows .Count != 0)
        {
            string[] result = new string[MyTb.Rows.Count];
            for (int i = 0; i < MyTb.Rows.Count; i++)
                result[i] = Convert.ToString(MyTb.Rows[i][0]) + "|" + Convert.ToString(MyTb.Rows[i][1]);
            return result;
        }
        else
        {
            return null;
        }
    }

    //默认数据绑定
    public void RoleBind(string sUserRole)
    {
        RoleName.DataSource = MyRM .GetMyCtrlRole (sUserRole,"1","");
        RoleName.DataBind();
        RoleName.Items.Insert(0, "选择功能角色");
        RoleName.Items[0].Value = "0";
    }

    public void DbRoleBind(string sDbRoleCode)
    {
        DbRoleName.DataSource = MyDRM .GetMyDbRoleInfo (sDbRoleCode);
        DbRoleName.DataBind();
        DbRoleName.Items.Insert(0, "选择数据角色");
        DbRoleName.Items[0].Value = "0";
    }

    public void AreaBind()
    {
        DDLArea.DataSource = bll.GetAreaList();
        DDLArea.DataBind();
        DDLArea.Items.Insert(0, "选择隶属区域");
        DDLArea.Items[0].Value = "0";
    }

    public void UserGroupBind()
    {
        /*UserGroup.DataSource = UMConn.BackView(15, "");
        UserGroup.DataBind();
        UserGroup.Items.Insert(0, "选择用户组");
        UserGroup.Items[0].Value = "0";*/
    }
    
    //默认数据绑定
    public void FirstpageBind(string sUserRole)
    {
        //first_Page.DataSource = UMConn.BackView(12, sUserRole);
        //first_Page.DataBind();
        //first_Page.Items.Insert(0, "选择首页");
        //first_Page.Items[0].Value = "0";
    }

    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        string sLogonID   = LogonID.Text.ToString().Trim();      //用户帐户
        string sHLogonID = HLogonID.Value.ToString().Trim();
        string sAlias     = Alias.Text.ToString().Trim();

        string PersID = HChoiceID_1.Value.ToString().Trim();  // hPersID1.Value.ToString().Trim();
        string[] sPersID = PersID.Split(',');

        string sHPersID = HChoiceID_1.Value.ToString().Trim();  // NewPersID1.Value.ToString().Trim();        //人员ID
        string[] sHPersIDD = sHPersID.Split(',');

        string sPersName = ChoiceName_1.Text.ToString().Trim();

        string sRoleCode  = RoleName.SelectedValue.ToString().Trim();
        string sDbRoleCode = HAjaxDropID.Value.ToString().Trim();
        string sFirstPage = first_Page.SelectedValue.ToString().Trim();
        string sCreatRole = (string)Session["Baimasoft_User_LogonID"];//Baimasoft_User_LogonID-Baimasoft_Role_Code
        string sEffective = Effective.SelectedValue.ToString().Trim();
        string sUserID    = UserID.Value.ToString().Trim();
        string sEditType  = hidEditType.Value.ToString().Trim();
        string sHUserGroup = HChoiceUserGroupID_1.Value.ToString(); // UserGroup.SelectedValue.ToString().Trim();//用户组

        string[] aHUserGroup = sHUserGroup.Split('^');
        string sUserGroup = aHUserGroup[1].ToString().Trim();

        string sResourceOrgID = HChoiceID_2.Value.ToString().Trim();  //资源树ID
        string sResourceOrgName = ChoiceName_2.Text.ToString().Trim();// 资源树名称


       /* try
        {
            if (sResourceOrgID == "")
            {
                sResourceOrgID = Session["TD_ResourceOrgID"].ToString().Trim();
            }
        }
        catch
        {
            sResourceOrgID = "";
        }*/

        if (sLogonID == "" || sLogonID == null)  //帐户名称
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户帐户不能为空!');</script>");
            return;
        }

        if (sPersID[0].ToString ().Trim () == "" || sPersID[0].ToString ().Trim () == null)  //人员名称
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('人员名称不能为空!');</script>");
            return;
        }

        if (sRoleCode.ToString().Trim() == "0")  //人员名称
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择功能角色!');</script>");
            return;
        }

        if (sEditType == "2")  //修改
        {
            if (sLogonID != sHLogonID)
            {
                int ibackLN = MyUM .GetLogonIDNum (sLogonID);
                if (ibackLN >0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐户名称重复!');</script>");                    
                }
            }

            if (sHPersIDD[0].ToString().Trim() != sPersID[0].ToString().Trim())
            {
                int ibackPD = MyUM .GetPersIDNum (sPersID[0].ToString().Trim());
                if (ibackPD > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此人员已创建了帐户!');</script>");
                    HOrgID.Value = "";
                    hPersID1.Value = "";
                    NewPersID1.Value = "";
                }
            }
            int iBack = MyUM .UpUserInfo (sUserID, sLogonID, sAlias, sPersID[0], sPersName, sRoleCode, sDbRoleCode, sFirstPage, sCreatRole, sEffective, sUserGroup, sResourceOrgID,sResourceOrgName );
            if (iBack >0)
            {
                bll.UpdateUserArea(sUserID, DDLArea.SelectedValue.Trim());
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
        else if (sEditType == "1")
        {
            int ibackLN = MyUM .GetLogonIDNum (sLogonID);
            if (ibackLN > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('帐户名称重复!');</script>");
                return;
            }
            int ibackPD = MyUM .GetPersIDNum (sPersID[0].ToString().Trim());
            if (ibackPD > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('此人员已创建了帐户!');</script>");
                HOrgID.Value = "";
                hPersID1.Value = "";
                NewPersID1.Value = "";
                return;
            }
            string sPassword = MyFile .Md5 ("111111");
            int iback2 = MyUM .InsertUserInfo (sLogonID, sAlias, sPassword, sPersID[0], sPersName, sRoleCode, sDbRoleCode, sFirstPage, sCreatRole, sEffective, sUserGroup, sResourceOrgID, sResourceOrgName);
            if (iback2 >0)
            {
                bll.UpdateUserArea(bll.MaxUserID(),DDLArea.SelectedValue.Trim());
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建失败!');</script>");
                return;
            }
        }
    }
}
